/*
 * Project Name: cms_web
 * File Name: SinglePageAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月23日 下午1:43:40
 */
package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.ArticleEntity;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.SinglePageEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.SinglePageService;

@Controller
@RequestMapping("/admin/singlePage")
public class SinglePageAction extends AdminBaseAction
{
	@Autowired
	private SinglePageService singlePageService;
	@InitBinder("singlePageEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("singlePageEntity.");
	}

	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addSinglePageEntity(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("single");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/singlePage/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewDownPage(SinglePageEntity singlePageEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception{
		singlePageEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (singlePageEntity.getTitle() == null
				|| "".equals(singlePageEntity.getTitle())) {
			json.getErrors().put("title", "标题不能为空");
		}
		if (singlePageEntity.getContent() == null
				|| "".equals(singlePageEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (singlePageEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		singlePageService.insert(singlePageEntity);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", singlePageService.getAllListPage(pageNum));
		return "system/singlePage/all";
	}
	
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delete(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		singlePageService.deleteById(id);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "id") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		SinglePageEntity singlePageEntity=singlePageService.getById(id);
		modelMap.put("singlePageEntity", singlePageEntity);
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("single");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/singlePage/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
			@ModelAttribute SinglePageEntity singlePageEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception {
		singlePageEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (singlePageEntity.getTitle() == null
				|| "".equals(singlePageEntity.getTitle())) {
			json.getErrors().put("title", "标题不能为空");
		}
		if (singlePageEntity.getContent() == null
				|| "".equals(singlePageEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (singlePageEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		SinglePageEntity oldSinglePageEntity=singlePageService.getById(singlePageEntity.getId());
		if (null==oldSinglePageEntity) {
			json.getErrors().put("title", "单页不存在");
			return json;
		}
		oldSinglePageEntity.setClassId(singlePageEntity.getClassId());
		oldSinglePageEntity.setContent(content);
		oldSinglePageEntity.setLastUpdateDate(new Date());
		oldSinglePageEntity.setTitle(singlePageEntity.getTitle());
		singlePageService.update(oldSinglePageEntity);
		json.setResult(true);
		return json;
	}
	
	
}
